package pay

import (
	"time"
	"dhfshop/models/member"
	"dhfshop/models/isv"
	"dhfshop/models/im"
	"dhfshop/models/org"
	"dhfshop/controllers/base"
	"github.com/astaxie/beego/validation"
	"github.com/pkg/errors"
	"github.com/astaxie/beego"
	"dhfshop/domain/pay"
)

// 支付管理
type PayController struct {
	base.BaseController
}

func (this *PayController) PayAddCommon() {
	var (
		curAppId   string = ""
		curOpenId  string = ""
		err        error
		acPay *isv.AcPayParams = nil
	)
	v := struct {
	    Kind     string   `form:"kind"     valid:"Required"`
		OrderId  string   `form:"order_id" valid:"Required"`
		Payment  float32  `form:"payment"  valid:"Required"`
		Body     string   `form:"body"     valid:"Required"`
	}{}
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return
	}
    curRoleId := this.Ctx.Input.GetData("curRoleId").(int) 
	curUserId := int64(this.Ctx.Input.GetData("curUserId").(int))
	partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	//beego.Debug("PayAddCommon payment:", v.Payment)
	if curRoleId == 1 {
	    curAppId = this.Ctx.Input.GetData("curAppId").(string)
		if members, err := member.GetMemberById(curUserId); err != nil {
			this.ServeError("GetMemberById:" + err.Error())
			return
		}else{
		    curOpenId = members.OpenID
		}
		acPay, err = isv.GetParamsByAppIdOrSubAppId(curAppId)
		if err != nil {
			err = errors.Wrap(err, "GetParamsByAppIdOrSubAppId... ")
			this.ServeError(err.Error())
			return
		}
	}else if curRoleId == 2 {
	    curAppId = beego.AppConfig.String("SellerAppID")
		if user, err := org.GetUserByUserId(curUserId); err != nil {
			this.ServeError("GetUserByUserId:" + err.Error())
			return   
		}else{
		    curOpenId = user.OpenID
		}
		acPay = new(isv.AcPayParams)
		acPay.MchTypeId = 1
		acPay.AppId = curAppId
		acPay.MchId = beego.AppConfig.String("SellerPayMchId")
		acPay.Appkey = beego.AppConfig.String("SellerPayAppKey")
	}else{
	    this.ServeError("cur login role is invalid")
		return
	}
	var prepayId string
	var payResponse paylayer.PayResponse
	if acPay.PayWayId == 9{
		payResponse, prepayId, err = paylayer.CommonRoyalPay(acPay.AppId,curOpenId,v.OrderId,v.Body,v.Kind,acPay,v.Payment)
		if err != nil {
		   this.ServeError(err.Error())
		   return
		}
	}else if acPay.PayWayId == 10{
	    payResponse, prepayId, err = paylayer.CommonYabandPay(acPay.AppId,curOpenId,v.OrderId,v.Body,v.Kind,acPay,v.Payment)
	    if err != nil {
		   this.ServeError(err.Error())
		   return
		}
	} else if acPay.PayWayId == 11 {
	    payResponse, prepayId, err = paylayer.CommonSuperPay(acPay.AppId,curOpenId,v.OrderId,v.Body,v.Kind,acPay,v.Payment)
	    if err != nil {
		   this.ServeError(err.Error())
		   return
		}
	} else if acPay.PayWayId == 12 {
	    payResponse, prepayId, err = paylayer.CommonUsagms(acPay.AppId,curOpenId,v.OrderId,v.Body,v.Kind,acPay,v.Payment)
	    if err != nil {
		   this.ServeError(err.Error())
		   return
		}
	} else if acPay.PayWayId == 13 {
	    payResponse, prepayId, err = paylayer.CommonDahlPay(acPay.AppId,curOpenId,v.OrderId,v.Body,v.Kind,acPay,v.Payment)
	    if err != nil {
		   this.ServeError(err.Error())
		   return
		}
	} else if acPay.PayWayId == 14 {
	    payResponse, prepayId, err = paylayer.CommonWiseCashier(acPay.AppId,curOpenId,v.OrderId,v.Body,v.Kind,acPay,v.Payment)
	    if err != nil {
		   this.ServeError(err.Error())
		   return
		}
	} else if acPay.PayWayId == 15 {
	    payResponse, prepayId, err = paylayer.CommonMegaezgoPay(acPay.AppId,curOpenId,v.OrderId,v.Body,v.Kind,acPay,v.Payment)
	    if err != nil {
		   this.ServeError(err.Error())
		   return
		}
	} else if acPay.PayWayId == 16 {
	    payResponse, prepayId, err = paylayer.CommonChinaUmsPay(acPay.AppId,curOpenId,v.OrderId,v.Body,v.Kind,acPay,v.Payment)
	    if err != nil {
		   this.ServeError(err.Error())
		   return
		}
	} else if acPay.PayWayId == 17 {
	    payResponse, prepayId, err = paylayer.CommonOceanPay(acPay.AppId,curOpenId,v.OrderId,v.Body,v.Kind,acPay,v.Payment)
	    if err != nil {
		   this.ServeError(err.Error())
		   return
		}
	} else if acPay.PayWayId == 18 {
	    payResponse, prepayId, err = paylayer.CommonQcurePay(acPay.AppId,curOpenId,v.OrderId,v.Body,v.Kind,acPay,v.Payment)
	    if err != nil {
		   this.ServeError(err.Error())
		   return
		}
	} else if acPay.PayWayId == 20 {
	    payResponse, prepayId, err = paylayer.CommonHnaPay(acPay.AppId,curOpenId,v.OrderId,v.Body,v.Kind,acPay,v.Payment)
	    if err != nil {
		   this.ServeError(err.Error())
		   return
		}
	} else if acPay.PayWayId == 21 {
	    payResponse, prepayId, err = paylayer.CommonAllinPay(acPay.AppId,curOpenId,v.OrderId,v.Body,v.Kind,acPay,v.Payment)
	    if err != nil {
		   this.ServeError(err.Error())
		   return
		}
	} else if acPay.PayWayId == 22 {
	    payResponse, prepayId, err = paylayer.CommonGpslmallPay(acPay.AppId,curOpenId,v.OrderId,v.Body,v.Kind,acPay,v.Payment)
	    if err != nil {
		   this.ServeError(err.Error())
		   return
		}
	} else {
		payResponse, prepayId, err = paylayer.CommonWxPay(curOpenId,v.OrderId,v.Body,v.Kind,acPay,v.Payment)
		if err != nil {
		   this.ServeError(err.Error())
		   return
		}
	}
	if len(prepayId) > 3 {
	    formIdCache := &im.FormIdCache{PartnerId:partnerId,MemberId:curUserId,AppId:curAppId,
						  FormId:prepayId,OpenId:curOpenId,Types:v.Kind,Xid:v.OrderId,
						  MaxCount:3,ExpireAt:time.Now().UTC().Add(time.Hour*24*7)}
		im.AddFormIdCache(formIdCache)
	}
	this.Data["json"] = payResponse
	this.ServeJSON()
}
